package com.frxuexi.mdm2206.myview

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.CycleInterpolator
import android.widget.ImageView
import android.widget.RelativeLayout
import com.frxuexi.mdm2206.R

class MyZanView:RelativeLayout {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x=event?.x
        val y=event?.y
        if(event?.action==MotionEvent.ACTION_DOWN){
            start(x,y)
        }
        return super.onTouchEvent(event)
    }

    private fun start(x: Float?, y: Float?) {
        val imageView = ImageView(context)
        imageView.setImageResource(R.drawable.ic_action_zan)
        val params = LayoutParams(200, 200)
        params.leftMargin=Math.round(x!!)-50
        params.topMargin=Math.round(y!!)-100
        imageView.layoutParams=params
        val animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f)
        val animator1 = ObjectAnimator.ofFloat(imageView, "rotation", x, 100f, x)
        animator1.interpolator=CycleInterpolator(5f)
        val animator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f)
        val animator3 = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f)
        val set = AnimatorSet()
        set.play(animator)
            .with(animator1)
            .with(animator2)
            .with(animator3)

        set.duration=3000
        set.start()
        addView(imageView)
    }
}